//
// Created by leisurexi on 12/9/20.
// thread2.c
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

void *thread_main(void *arg) {
    int i;
    int cnt = *((int *) arg);
    char *msg = (char *) malloc(sizeof(char) * 50);
    strcpy(msg, "Hello, I'am thread~ \n");

    for (i = 0; i < cnt; i++) {
        sleep(1);
        puts("running thread");
    }
    return (void *) msg;
}

int main(int argc, char *argv[]) {
    pthread_t t_id;
    int thread_param = 5;
    void *thread_result;

    if (pthread_create(&t_id, NULL, thread_main, (void *) &thread_param) != 0) {
        puts("pthread_create() error");
        return -1;
    }

    if (pthread_join(t_id, &thread_result) != 0) {
        puts("pthread_join() error");
        return -1;
    }

    printf("Thread return message: %s \n", (char *) thread_result);
    free(thread_result);
    return 0;
}
